package com.example.notebookapp.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.notebookapp.R;
import com.example.notebookapp.bd.MyDbHelper;
import com.example.notebookapp.bean.Notepad;
import com.example.notebookapp.databinding.ActivityEditBinding;
import com.example.notebookapp.utils.TimeUtil;

import java.io.Serializable;

public class EditActivity extends AppCompatActivity {

    private ActivityEditBinding binding;

    private MyDbHelper myDbHelper;
    private Notepad notepad;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        binding = ActivityEditBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        Serializable serializable = getIntent().getSerializableExtra("notepad");
        if (serializable == null){
            binding.titleTv.setText("添加记录");
        }else {
            binding.titleTv.setText("修改记录");
            notepad = (Notepad) serializable;
            binding.contentEt.setText(notepad.getContent());
        }

        binding.titleTv.setText("添加记录");
        binding.backIv.setOnClickListener(v -> finish());

        myDbHelper = new MyDbHelper(this);

        //正常写法
//        binding.clearIv.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                binding.contentEt.setText("");
//            }
//        });

//简化写法
        binding.deleteIv.setOnClickListener(v -> binding.contentEt.setText(""));

        binding.saveIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取输入框的内容
                String content = binding.contentEt.getText().toString().trim();
                //非空判断-友好提示
                if (TextUtils.isEmpty(content)){
                    Toast.makeText(EditActivity.this, "请输入内容", Toast.LENGTH_SHORT).show();
                    return;
                }


//                判断是新增还是修改


                if (notepad == null){
                    notepad = new Notepad();
                    notepad.setContent(content);
                    notepad.setTime(TimeUtil.getTime());
                    myDbHelper.insert(notepad);
                    Toast.makeText(EditActivity.this,"新增成功",Toast.LENGTH_SHORT).show();
                }else {
                    notepad.setContent(content);
                    notepad.setTime(TimeUtil.getTime());
                    myDbHelper.update(notepad);
                    Toast.makeText(EditActivity.this,"更新成功",Toast.LENGTH_SHORT).show();
                }
//                //封装数据
//                Notepad notepad = new Notepad();
//                notepad.setContent(content);
//                notepad.setTime(TimeUtil.getTime());
//                //插入数据库
//                myDbHelper.insert(notepad);
//                Toast.makeText(EditActivity.this, "新增成功", Toast.LENGTH_SHORT).show();
                setResult(RESULT_OK);
                finish();
            }
        });
    }
}